package chapter3.No3_1_等待和通知机制.j生产者_消费者模式实现.demo1;

/**
 * Created by Administrator on 2016/11/19.
 */
public class P {
    Object lock;

    public P(Object lock) {
        this.lock = lock;
    }

    /**
     * 如果有一个值就等待，否则插入一个值
     */
    public void setValue() {
        synchronized (lock) {
            try {
                if(!ValueObject.value.equals("")) {
                    lock.wait();
                }
                // set Value
                String value = System.currentTimeMillis()+"_"+System.nanoTime();
                System.out.println("set value:"+value);
                ValueObject.value = value;
                // 通知消费者去消费
                lock.notify();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(System.nanoTime());
        System.out.println(System.currentTimeMillis());
    }
}
